# _*_ coding : utf-8 _*_
# @Time : 2023/4/8 0008 14:05
# @Author : 菜鸟王小二
# @File : 40_selenium自动化商城添加订单
# @Project : python爬虫
import time

from selenium.webdriver.common.by import By
from webdriver_helper import get_webdriver

# 可以下载webdriver_helper==1.0.1免费版，导入get_webdriver，此插件自动获取浏览器驱动、版本
with get_webdriver() as driver:
    driver.get("http://101.34.221.219:8010/")
    driver.maximize_window()
    driver.implicitly_wait(5)

    # 找到登录按钮，点击
    driver.find_element(By.XPATH,"//a[text()='登录']").click()
    time.sleep(1)

    driver.find_element(By.XPATH,"//input[@placeholder='请输入用户名/手机/邮箱']").send_keys('xiaoer666')
    driver.find_element(By.XPATH,"//input[@placeholder='请输入登录密码']").send_keys('123456')
    driver.find_element(By.XPATH,'//button[text()="登录"]').click()
    # 点击后等待0.5秒，浏览器没有那么快显示提示信息
    time.sleep(.5)
    msg = driver.find_element(By.CLASS_NAME,'prompt-msg').text
    print(msg)
    assert msg == '登录成功'

    # 登录成功后，跳转回首页，需等待一会
    time.sleep(2)
    # 往下滑动一页面一半
    driver.execute_script("window.scrollTo(0,document.body.scrollHeight/2)")
    time.sleep(1)
    # 找到以文本'MARNI'开头的商品，点击
    driver.find_element(By.XPATH,"//a[starts-with(text(),'MARNI')]").click()

    # 此时跳转到新的窗口
    driver.switch_to.window(driver.window_handles[-1])
    # 点击数量+，添加为2件
    driver.find_element(By.ID,"add").click()
    # 点击立即购买
    driver.find_element(By.CLASS_NAME,'buy-btn,buy-submit,buy-event,login-event').click()

    # 跳转后解决弹窗问题
    time.sleep(.5)
    driver.switch_to.alert.accept()

    # 点击货到付款
    driver.find_element(By.XPATH,"//li/span[text()='货到付款']").click()
    driver.switch_to.alert.accept()
    # 点击提交订单
    driver.find_element(By.XPATH,'//button[@title="点击此按钮，提交订单"]').click()

    time.sleep(.5)
    msg = driver.find_element(By.CLASS_NAME, 'prompt-msg').text
    print(msg)
    assert msg == '操作成功'
